﻿using System.Collections.Generic;
using System.Data;
using MedicService.Domain;
using MedicService.Server.Factory.Generic;

namespace MedicService.Server.Factory {
    public class QuestionFactory : EntityFactory<Question> {
        public QuestionFactory() : base("Questions") { }

        protected override void FillEntity(Question entity, DataRow myDataRow) {
            entity.Id = (int) myDataRow["id"];
            entity.Context = (string)myDataRow["context"];
            entity.GroupExp = new GroupExpFactory().Load(myDataRow["groupexpid"]);
        }

        protected override string GetInsertQuery(Question entity) {
            string q = "INSERT INTO [Questions] ([context],[groupexpid])"
                     + " VALUES ("
                     +Nullable(entity.Context)+","+Nullable(entity.GroupExp)+");"
                     + " ";
            return q;
        }

        protected override string GetUpdateQuery(Question entity) {
            string q = "UPDATE [Questions]"
                     + "   SET [context] = " + Nullable(entity.Context)
                     + "      ,[groupexpid] = " + Nullable(entity.GroupExp) 
                     + " WHERE id" + entity.Id;
            return q;
        }

        public List<Question> GetList(GroupExp groupExp) {
            var entities = new List<Question>();
            var dataTable = DatabaseConnector.GetData(TableName, OrderBy, " groupexpid=" + groupExp.Id);
            foreach (DataRow myDataRow in dataTable.Rows) {
                var entity = CreateEntity();
                FillEntity(entity, myDataRow);
                entities.Add(entity);
            }
            return entities;
        }

        protected override Question CreateEntity() {
            return new Question();
        }

    }
}